from rest_framework.decorators import api_view
from rest_framework.response import Response
from student.models import StudentData
from ocr.face.core import face_orc

@api_view(["POST"])
def faceid(request):
    sfz = request.data.get("sfz",None)
    pic = request.data.get("pic",None)
    sfzid = request.data.get("sfzid",None)
    if not (sfz and pic and sfzid):
        return Response({
            "result":False,
            "message":"请拍摄身份证",
            "code":40001
        })
    success,data = face_orc(sfz,pic)
    data["Score"] = round(data["Score"],2)
    if success:
        # sfzid = data["sfzid"]
        # name = data["name"]
        stu = StudentData.objects.filter(sfzid=sfzid).first()
        if not stu:
            return Response({
                "result":False,
                "message":"请先导入学生数据",
                "code":40002
            })
        stu.sfz_img = sfz
        stu.result = data["Score"]
        stu.save()
        return Response({
            "result":success,
            "message":"ok",
            "data":data
        })
    return Response({
        "result":False,
        "message":"未知错误,联系开发者",
        "code":40002
    })
    

